package net.zdsoft.wpchat.support.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import net.zdsoft.keel.util.ArrayUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserConfig{
	private static Logger logger = LoggerFactory.getLogger(UserConfig.class);

	public static final String ROLES_ADMIN = "超级管理员";
	public static final String ROLES_SALES = "销售人员";
	public static final String ROLES_BUSINESS = "商务人员";
	public static final String ROLES_EXECUTER = "实施人员";
	public static final String ROLES_SUPPORT = "运营人员";
	public static final String ROLES_MANAGER = "项目管理";
	public static final String ROLES_FINANCIAL = "财务人员";
	public static final String ROLES_AGENT = "代理商";

	private final HashMap<String, UserCatalog> userCatalogMap = new HashMap<String, UserCatalog>();
	private final ArrayList<UserCatalog> userCatalogList = new ArrayList<UserCatalog>();
	private final HashMap<String, UserOperation> operationMapKeyByAction = new HashMap<String, UserOperation>();

	private String[] roles;

	public UserConfig() {
	}

	public UserCatalog putUserCatalog(UserCatalog userCatalog) {
		String catalogId = userCatalog.getCatalog().getId();

		UserCatalog _userCatalog = userCatalogMap.get(catalogId);

		if (_userCatalog == null) {
			logger.debug("Put catalog[" + userCatalog.getCatalog().getName()
					+ "]");

			userCatalogMap.put(catalogId, userCatalog);
			userCatalogList.add(userCatalog);
			return userCatalog;
		}

		return _userCatalog;
	}

	public List<UserCatalog> getUserCatalogs() {
		return userCatalogList;
	}

	// 注册operation，使得能够通过action的名字检索到
	public void registerOperation(UserOperation userOperation) {
		String[] actions = userOperation.getOperation().getAction();

		for (int i = 0; i < actions.length; i++) {
			String actionName = actions[i];

			if (actionName.indexOf(".") == -1) {
				actionName = userOperation.getOperation().getModule()
						.getCatalog().getNamespace()
						+ "." + actionName;
			}

			logger.debug("Register action[" + actionName + "]");

			operationMapKeyByAction.put(actionName, userOperation);
		}
	}

	public UserOperation getUserOperation(String packageName, String actionName) {
		return operationMapKeyByAction.get(packageName + "." + actionName);
	}

	public void orderCatalogByVisibleIndex() {
		Collections.sort(userCatalogList, new Comparator<UserCatalog>() {
			@Override
			public int compare(UserCatalog o1, UserCatalog o2) {
				AuthCatalog catalog0 = o1.getCatalog();
				AuthCatalog catalog1 = o2.getCatalog();
				return catalog0.getDisplayOrder() - catalog1.getDisplayOrder();
			}
		});
	}

	public String[] getRoles() {
		return roles;
	}

	public void setRoles(String[] roles) {
		this.roles = roles;
	}

	/**
	 * 是否是代理商
	 *
	 * @return
	 */
	public boolean isAgent() {
		return ArrayUtils.contains(roles, ROLES_AGENT);
	}

	/**
	 * 是否是超级管理员
	 *
	 * @return
	 */
	public boolean isAdmin() {
		return ArrayUtils.contains(roles, ROLES_ADMIN);
	}

	/**
	 * 是否是销售人员
	 *
	 * @return
	 */
	public boolean isSales() {
		return ArrayUtils.contains(roles, ROLES_SALES);
	}

	/**
	 * 是否是商务人员
	 *
	 * @return
	 */
	public boolean isBusiness() {
		return ArrayUtils.contains(roles, ROLES_BUSINESS);
	}

	/**
	 * 是否是实施人员
	 *
	 * @return
	 */
	public boolean isExecuter() {
		return ArrayUtils.contains(roles, ROLES_EXECUTER);
	}

	/**
	 * 是否是运营人员
	 *
	 * @return
	 */
	public boolean isSupport() {
		return ArrayUtils.contains(roles, ROLES_SUPPORT);
	}

	/**
	 * 是否是财务人员
	 *
	 * @return
	 */
	public boolean isFinancial() {
		return ArrayUtils.contains(roles, ROLES_FINANCIAL);
	}

	/**
	 * 是否是项目管理
	 *
	 * @return
	 */
	public boolean isManager() {
		return ArrayUtils.contains(roles, ROLES_MANAGER);
	}

}
